package com.sinosoft.web.common.pushlet;

import nl.justobjects.pushlet.core.Dispatcher;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventSource;
import nl.justobjects.pushlet.util.Log;

/**
 * Created by Administrator on 2016/6/21.
 */
public class EventPullSources {

    static public class MessageEventPullSource implements EventSource, Runnable {
        private volatile boolean alive = false;
        private volatile boolean active = false;
        private static int threadNum = 0;
        private Thread thread;

        public MessageEventPullSource() {
        }

        public void start() {
            thread = new Thread(this, "MyEventPullSource-" + (++threadNum));
            thread.setDaemon(true);
            thread.start();
        }

        public boolean isAlive() {
            return alive;
        }

        /**
         * Stop the event generator thread.
         */
        public void stop() {
            alive = false;

            if (thread != null) {
                thread.interrupt();
                thread = null;
            }

        }

        /**
         * Activate the event generator thread.
         */
        synchronized public void activate() {
            if (active) {
                return;
            }
            active = true;
            if (!alive) {
                start();
                return;
            }
            Log.debug(getClass().getName() + ": notifying...");
            notifyAll();
        }

        /**
         * Deactivate the event generator thread.
         */
        public void passivate() {
            if (!active) {
                return;
            }
            active = false;
        }

        /**
         * Main loop: sleep, generate event and publish.
         */
        public void run() {
            Log.debug(getClass().getName() + ": starting...");
            alive = true;
            while (alive) {
                try {

                    Thread.sleep(3000);

                    // Stopped during sleep: end loop.
                    if (!alive) {
                        break;
                    }

                    // If passivated wait until we get
                    // get notify()-ied. If there are no subscribers
                    // it wasts CPU to remain producing events...
                    synchronized (this) {
                        while (!active) {
                            Log.debug(getClass().getName() + ": waiting...");
                            wait();
                        }
                    }

                } catch (InterruptedException e) {
                    break;
                }

                try {
                    // Derived class should produce an event.
                    Event event = Event.createDataEvent("/message/test");
                    Dispatcher.getInstance().multicast(event);
                } catch (Throwable t) {
                    Log.warn("EventPullSource exception while multicasting ", t);
                    t.printStackTrace();
                }
            }
            Log.debug(getClass().getName() + ": stopped");
        }
    }


}
